﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using Sherwood.Configuration;
using Sherwood.SignOn.Server.Models.Repositories;
using System.Text.RegularExpressions;
using Sherwood.SignOn.Server.Models.Configuration;

namespace Sherwood.SignOn.Server.Models.Services
{
    /// <summary>
    /// Provides access to global application data.
    /// </summary>
    public class Data
    {

        private static IClientRepository _clientRepository;
        private static IUserAccountRepository _userAccounts;
        private static IUserSessionRepository _userSessions;
        private static IUserProfileRepository _userProfiles;
        private static IConfigRepository _configRepository;
        

        /// <summary>
        /// Provides access to user profiles from other systems.
        /// </summary>
        public static IUserProfileRepository UserProfiles
        {
            get
            {
                if (_userProfiles == null)
                {
                    _userProfiles = DependencyInjection.Resolve<IUserProfileRepository>();
                }
                return _userProfiles;
            }

        }

        /// <summary>
        /// Current client repository for the application.
        /// A client refers to a machine or system client which is allowed to interact with the 
        /// the system.
        /// </summary>
        public static IClientRepository Clients
        {
            get
            {
                if (_clientRepository == null)
                {
                    _clientRepository = DependencyInjection.Resolve<IClientRepository>();
                }
                return _clientRepository;
            }
            set
            {
                _clientRepository = value;
            }
        }

        /// <summary>
        /// Gets/sets the user account repository for the application.
        /// </summary>
        public static IUserAccountRepository UserAccounts
        {
            get
            {
                if (_userAccounts == null)
                {
                    _userAccounts = Sherwood.DependencyInjection.Resolve<IUserAccountRepository>();
                }
                return _userAccounts;
            }
            set
            {
                _userAccounts = value;
            }
        }


        /// <summary>
        /// Gets/sets the repository used to track user sessions.
        /// </summary>
        public static IUserSessionRepository UserSessions
        {
            get { return _userSessions ?? (_userSessions = DependencyInjection.Resolve<IUserSessionRepository>()); }
            set
            {
                _userSessions = value;
            }
        }


        /// <summary>
        /// Returns the default configuration repository.
        /// </summary>
        public static IConfigRepository ConfigRepository
        {

            get { return _configRepository ?? (_configRepository = DependencyInjection.Resolve<IConfigRepository>()); }
        }

        public static string GetLanguageLink(string language)
        {
            string url = HttpContext.Current.Request.Url.AbsoluteUri;
            Regex relang = new Regex("&?lang=..-..");
            url = relang.Replace(url, "");
            if (url.Contains("?"))
            {
                url = url + "&lang=" + language;
            }
            else
            {
                url = url + "?lang=" + language;
            }
            return url;
        }


    }
}
